public class Solution {
    // Parameters:
    //    numbers:     an array of integers
    //    length:      the length of array numbers
    //    duplication: (Output) the duplicated number in the array number,length of duplication array is 1,so using duplication[0] = ? in implementation;
    //                  Here duplication like pointor in C/C++, duplication[0] equal *duplication in C/C++
    //    这里要特别注意~返回任意重复的一个，赋值duplication[0]
    // Return value:       true if the input is valid, and there are some duplications in the array number
    //                     otherwise false
    public static void exchange(int i, int j, int[] array){
        int temp = array[i];
        array[i] =array[j];
        array[j] =temp;
    }
    public static boolean duplicate(int numbers[],int length,int [] duplication) {
        if(numbers.length<=1){
            duplication[0] = -1;
            return false;
        }
        for(int i=0; i<numbers.length; i++){
            if(numbers[i]<0 || numbers[i]>numbers.length-1){
                duplication[0] = -1;
                return false;
            }

        }
        for(int i=0; i<numbers.length; i++){
            while(numbers[i]!=i){
                if(numbers[numbers[i]]==numbers[i]){//还没换的时候，发现该位置已经有值。
                    duplication[0]=numbers[i];
                    return true;
                }
                exchange(i,numbers[i],numbers);
            }
        }
        duplication[0] = -1;
        return false;
    }
    public static void main(String args[]){
        int[] duplication = new int[1];
        int[] numbers = {2,3,1,0,2,5,3};
        boolean b = duplicate(numbers,numbers.length,duplication);
        System.out.println(b);
    }
}

